<template>
  <!-- 申请子账号 -->
  <div class="box">
    <div class="apply_title">申请子账号</div>
    <div class="apply_tip">
      根据会员等级申请子账号，优塑会员可申请1+2，优企会员可申请1+6。
    </div>
    <button class="apply_btn" @click="addNewAccount">新增子账号</button>
    <div class="apply_table">
      <div class="apply_table_column">
        <div
          class="apply_table_title"
          v-for="(item, index) in tableColumn"
          :key="index"
        >
          {{ item }}
        </div>
      </div>
      <el-divider></el-divider>
      <div
        class="apply_table_info"
        v-for="(item, index) in tableData"
        :key="index"
      >
        <div class="apply_info_box">
          <div class="apply_info_tip" v-if="item.isMember == 1">主账号</div>
          <div class="apply_info_tip son" v-else>子账号</div>
          <div class="apply_info_text">{{ item.loginName }}</div>
          <!-- <div class="apply_info_text">{{ options[Number(item.type) - 1].label }}</div> -->
          <div class="apply_info_text">{{ item.type }}</div>
          <div class="apply_info_text">{{ item.accountRemark || "无" }}</div>
          <div class="apply_info_btn">
            <div>
              <button class="btn_revise" @click="editRemark(item.id)">
                修改备注
              </button>
              <div class="box-btn">
                <button
                  @click="showdelDialog(index)"
                  class="btn_del"
                  v-show="item.isMember == 1 ? false : true"
                >
                  删除
                </button>
              </div>
            </div>
          </div>
        </div>
        <el-divider></el-divider>
      </div>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="delDialog"
      width="5.71rem"
      center
      class="del_dialog"
    >
      <span>是否删除此条信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialog = false">取消</el-button>
        <el-button type="primary" @click="deleteMember">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="新增子账号"
      :visible.sync="addDialog"
      width="5.71rem"
      center
      class="add_dialog"
    >
      <el-form ref="form" :model="form" v-if="!isEdit">
        <el-form-item label="账户手机">
          <el-input
            v-model="form.loginName"
            placeholder="请输入登录账号的手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model="form.type" placeholder="请输入职位名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="角色配置">
          <el-select v-model="form.type" placeholder="请选择角色配置">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="账户备注">
          <el-input
            v-model="form.accountRemark"
            placeholder="请输入账户备注"
          ></el-input>
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input
            type="password"
            v-model="form.loginPwd"
            placeholder="请输入登录密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input
            type="password"
            v-model="form.sure"
            placeholder="请输入确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="form.code" placeholder="请输入验证码"> </el-input>
          <el-button
            v-if="time == 0"
            class="btn_code"
            @click="$noMultipleClicks(getSmsCodeByAliyun)"
            >获取手机验证码</el-button
          >
          <el-button v-else class="btn_code">{{ time }}s后可重发</el-button>
        </el-form-item>
      </el-form>
      <el-form v-else ref="accountRemark" :model="accountRemark">
        <el-form-item label="账户备注">
          <el-input
            v-model="accountRemark.remark"
            placeholder="请输入账户备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="addMember">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.info = JSON.parse(sessionStorage.getItem("userInfo"));
    this.listMember();
    // let timestamp = sessionStorage.getItem("timestamp");
    // console.log(Date.now() - timestamp < 60 * 1000);
    // if (Date.now() - timestamp < 60 * 1000) {
    //   this.time = 60 - parseInt((Date.now() - timestamp) / 1000);
    //   this.setCodeTimer();
    // }
    this.$getCodeTimer();
    this.getUserInfo();
  },
  data() {
    return {
      isEdit: false,
      info: {},
      tableColumn: ["账号", "职位", "备注", "操作"],
      tableData: [],
      delDialog: false,
      addDialog: false,
      time: 0,
      accountRemark: {
        remark: "",
      },
      form: {
        loginName: "",
        type: "",
        accountRemark: "",
        loginPwd: "",
        sure: "",
        code: "",
      },
      delIndex: -1,
      editId: -1,
      show: true,
      noClick: true,
      userLevel: {},
    };
  },
  methods: {
    cancel() {
      this.addDialog = false;
    },

    // 修改备注
    editRemark(id) {
      this.accountRemark.remark = "";
      this.isEdit = true;
      this.addDialog = true;
      this.editId = id;
    },

    async getUserInfo() {
      let res = await this.$apiFun.getUserInfo();
      this.userLevel = res.result;
      // console.log("this.userLevel", this.userLevel);
    },

    addNewAccount() {
      // console.log(this.userLevel.identityType);
      if (this.userLevel.identityType == 1) {
        this.$message.error(
          "当前账号等级为普通用户，不能申请子账号，优塑会员可申请1+2，优企会员可申请1+6。"
        );
      } else if (this.userLevel.identityType == 2) {
        this.$message.error(
          "当前账号等级为普通会员,不能申请子账号，优塑会员可申请1+2，优企会员可申请1+6。"
        );
      } else {
        this.addDialog = true;
        this.isEdit = false;
      }
    },

    // setCodeTimer() {
    //   if (this.time == 0) {
    //     sessionStorage.setItem("timestamp", Date.now());
    //     this.time = 60;
    //   }

    //   let timer = setInterval(() => {
    //     this.time -= 1;
    //     if (this.time <= 0) {
    //       clearInterval(timer);
    //     }
    //   }, 1000);
    // },
    showdelDialog(index) {
      this.delIndex = index;
      this.delDialog = true;
    },

    // 接口请求
    // 阿里云验证码
    async getSmsCodeByAliyun() {
      if (!/^1[3456789]\d{9}$/.test(this.form.loginName)) {
        this.$message.error("请输入正确的手机号码");
      } else {
        let res = await this.$apiFun.getSmsCodeByAliyun({
          mobile: this.form.loginName,
        });
        if (res.code == 200) {
          this.$message({
            type: "success",
            duration: 1500,
            message: "获取成功",
          });
          this.$setCodeTimer();
        } else {
          this.$message({
            type: "error",
            duration: 1500,
            message: "获取失败",
          });
        }
      }
    },
    // 子账号列表
    async listMember() {
      let res = await this.$apiFun.listMember({
        parentId: this.info.id,
      });
      // this.tableData = res.result.rows;
      this.tableData = res.result.rows.map((row) => {
        return { ...row };
      });
      // this.total = Number(res.result.total)
    },
    // 添加子账号
    async addMember() {
      // 修改备注
      if (this.isEdit) {
        if (this.accountRemark.remark == "") {
          this.$message.error("请填写账户备注");
        } else {
          let res = await this.$apiFun.updateRemark({
            accountRemark: this.accountRemark.remark,
            id: this.editId,
          });
          if (res.code == 200) {
            this.$message({
              type: "success",
              duration: 1500,
              message: "修改成功",
            });
            this.isEdit = false;
            this.addDialog = false;
            this.listMember();
          }
        }

        return;
      }

      let message = "";
      if (!/^1[3456789]\d{9}$/.test(this.form.loginName)) {
        message = "请输入正确的手机号";
      } else if (this.form.type == "") {
        message = "请输入职位名称";
      } else if (this.form.accountRemark == "") {
        message = "请填写账户备注";
      } else if (this.form.loginPwd == "") {
        message = "请填写登录密码";
      } else if (this.form.sure == "") {
        message = "确认密码不能为空";
      } else if (this.form.loginPwd !== this.form.sure) {
        message = "两次输入的密码不一致";
      } else if (this.form.code == "") {
        message = "验证码不能为空";
      } else if (this.form.code.length !== 6) {
        message = "请输入六位数的验证码";
      } else {
        let res = await this.$apiFun.addMember({
          // parentId: this.info.id,
          ...this.form,
          mobile: this.form.loginName,
        });
        if (res.code == 200) {
          this.$message({
            type: "success",
            duration: 1500,
            message: "添加成功",
          });
          this.addDialog = false;
          this.form.loginName = this.form.type = this.form.accountRemark = this.form.loginPwd =
          this.form.sure = this.form.code = "";
        }
        this.listMember();
      }

      if (message) {
        this.$message.error(message);
      }
    },
    async deleteMember() {
      let id = this.tableData[this.delIndex].id;
      let res = await this.$apiFun.deleteMember({ id });
      if (res.code == 200) {
        this.$message({
          type: "success",
          duration: 1500,
          message: "删除成功",
        });
        this.tableData.splice(this.delIndex, 1);
      }
      this.delDialog = false;
    },

    // showBtn(index) {
    //   this.tableData[index].show = true;
    // },
    // hideBtn(index) {
    //   this.tableData[index].show = false;
    // },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-divider--horizontal {
  margin: 0.1rem 0rem;
}
/deep/.el-dialog {
  margin-top: 2rem !important;
  border-radius: 10px;
  .el-dialog__header {
    border-radius: 10px 10px 0px 0px;
    background: #efefef;
    display: flex;
    justify-content: space-between;
    .el-dialog__title {
      margin-left: 0.3rem;
      font-size: 0.2rem;
      font-weight: bold;
      color: #333333;
    }
  }
  .el-dialog__footer {
    height: 0.7rem;
    padding: 0;
    .dialog-footer {
      width: 100%;
      height: 0.4rem !important;
      display: flex;
      justify-content: center;

      .el-button--default {
        width: 2rem;
        height: 0.4rem;
        line-height: 0.1rem;
        background: #ffffff;
        border: 1px solid #333333;
        border-radius: 5px;
        display: block;
      }
      .el-button--primary {
        width: 2rem;
        height: 0.4rem;
        line-height: 0.1rem;
        background: #f42551;
        border-radius: 5px;
        border: none;
        margin-left: 0.3rem;
        display: block;
      }
    }
  }
}
.box {
  background: #ffffff;
  box-shadow: 0px 3px 20px 0px rgba(230, 230, 230, 0.8);
  border-radius: 10px;
  padding: 0.3rem 0.5rem 1.5rem;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  font-family: Microsoft YaHei;

  .apply_title {
    font-size: 0.24rem;
    font-weight: bold;
    color: #333333;
    text-align: left;
  }
  .apply_tip {
    width: 6rem;
    margin-top: 0.2rem;
    padding: 0.1rem 0rem;
    background: #efefef;
    border-radius: 5px;
    font-size: 0.18rem;
    font-weight: 400;
    color: #666666;
  }
  .apply_btn {
    margin-top: 0.2rem;
    width: 2.3rem;
    height: 0.4rem;
    background: #f42551;
    border-radius: 5px;
    border: none;
    font-size: 0.16rem;
    font-weight: bold;
    color: #ffffff;
    line-height: 0.4rem;
    cursor: pointer;
  }
  .apply_table {
    margin-top: 0.5rem;
    .apply_table_column {
      display: flex;
      .apply_table_title {
        flex: 1;
        font-size: 0.16rem;
        font-weight: bold;
        color: #333333;
        line-height: 0.3rem;
      }
    }
    .apply_table_info {
      .apply_info_box {
        display: flex;
        position: relative;
        .apply_info_text {
          flex: 1;
          font-size: 0.16rem;
          font-weight: 400;
          color: #333333;
          line-height: 0.3rem;
        }
        .apply_info_btn {
          flex: 1;

          div {
            width: 100%;
            height: 0.3rem;
            display: flex;
            justify-content: space-evenly;
            .btn_revise {
              background: none;
              border: none;
              font-size: 0.16rem;
              font-weight: bold;
              color: #333333;
              line-height: 0.3rem;
              cursor: pointer;
            }
            .box-btn {
              width: 0.44rem;
              height: 0.3rem;
              .btn_del {
                background: none;
                border: none;
                font-size: 0.16rem;
                font-weight: bold;
                color: #f42551;
                line-height: 0.3rem;
                cursor: pointer;
                padding: 0;
              }
            }
          }
        }
        .apply_info_tip {
          position: absolute;
          left: 2rem;
          font-size: 0.16rem;
          font-weight: bold;
          color: #f42551;
          line-height: 0.3rem;
          opacity: 0.7;
        }
        .son{
          color: #000;
        }
      }
    }
  }
  .del_dialog {
    /deep/.el-dialog {
      .el-dialog__body {
        text-align: center;
        span {
          font-size: 0.2rem;
          font-weight: 400;
          color: #333333;
        }
      }
    }
  }
  .add_dialog {
    /deep/.el-dialog__body {
      padding: 0.25rem 0.5rem 0rem;
      .el-form {
        .el-form-item {
          display: flex;
          .el-form-item__content {
            flex: 1;
          }
          .el-form-item__label {
            text-align: left;
            width: 0.9rem;
          }
          .el-select {
            width: 100%;
          }
          .el-button {
            width: 1.2rem;
            position: absolute;
            right: 0.3rem;
            font-size: 0.16rem;
            font-weight: 400;
            text-decoration: underline;
            color: #f42551;
            border: none;
            background: transparent;
          }
        }
      }
    }
  }
}
</style>